﻿using Abp.Dependency;
using Mt.Site.Core.Organizations.UserCache;
using Mt.Site.Core.WorkFlows.Design;
using Mt.Site.Core.WorkFlows.Process;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Mt.Site.Core.WorkFlows.Diy.Plugin.Impl
{
    public class SkipUnitHeaderNode : WorkFlowNodePluginFunctionsBase, ITransientDependency
    {
        public const string Name = "WFNode.SkipUnitHeaderNode";

        public override string DisplayName => "主管起草稿件跳过主管审核";

        public override string UniqeName => Name;

        public override async Task<List<IWorkflowNodeDefinition>> CheckNextNodeListAsync(List<IWorkflowNodeDefinition> list, IWorkFlowManager workFlowManager)
        {
            var paper = workFlowManager.CurrentPaper.Paper;

            //副职主持工作， 正职，及可能的副职领导跳过部室审核
            var userId = paper.CreatorUserId.Value;
            var userInfo = await workFlowManager.UserAndUnitFinder.GetUserInfoAsync(userId);

            long unitId;
            if (paper.UnitId > 0)
            {
                unitId = paper.UnitId;
            }
            else
            {
                unitId = userInfo.MainOrgnzation.Id;
            }
            
            if (userInfo.IsDepartmentHeadOrDeputyHeadPresiding(unitId))
            {
                if (list.Count > 1)
                {
                    //流程配置时需调整好顺序
                    list.RemoveAt(0);
                }
            }

            return list;
        }
    }
}
